<?php
	//include('../global.php');
	set_time_limit(0);
	//error_reporting(0);
	
	function doget ($url, $referer, $cookie) // get获取数据使用 
	{
		$optionget = array('http' => array('method' => "GET", 'header' => "User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11\r\nAccept:*/*\r\nReferer:" . $referer . "\r\nCookie:" . $cookie)); 
		$file = file_get_contents($url, false , stream_context_create($optionget)); 
		return $file; 
	} 

	if($_GET['id']&&$_GET['num']&&$_GET['id']<2000){
		$cookies = "anonymid=h5vty0cpn6hsma; _r01_=1; l4pager=1; mop_uniq_ckid=127.0.0.1_1345016288_481154060; pgv_pvi=8995215360; __utma=10481322.1320977248.1345080944.1345080944.1345080944.1; __utmz=10481322.1345080944.1.1.utmcsr=renren.com|utmccn=(referral)|utmcmd=referral|utmcct=/277974863/profile; XNESSESSIONID=b580ba7ebf57; jebecookies=257b8052-72da-40d6-be90-11951afeb8a2|||||; ick_login=acd80a3d-8bc3-47dd-9333-c37c85378104; ick=31d855c6-ae5e-4eb1-9d83-6b2b536c3d15; newbie=1; t=50e62c88fee1124ac191ee9b0aa121939; societyguester=50e62c88fee1124ac191ee9b0aa121939; id=476901189; xnsid=c7360032; idc=tel; loginfrom=null; JSESSIONID=3E5B219E6C95B6E363A0D2A64C9143F6";
		$url = "http://browse.renren.com/sAjax.do?ajax=1&q=&p=%5B%7B%22id%22%3A%2213005%22%2C%22t%22%3A%22univ%22%2C%22name%22%3A%22%E6%B9%96%E5%8D%97%E5%86%9C%E5%A4%A7%22%7D%2C%7B%22gend%22%3A%22%E5%A5%B3%E7%94%9F%22%2C%22t%22%3A%22base%22%7D%5D&s=0&u=476901189&act=search&offset=".$_GET['id'];
		$contents = doget($url,"http://browse.renren.com/search.do?ref_search=searchResult_ReSearch",$cookies);
		$contents = tansHTML($contents);
		//采集姓名
		preg_match_all("/target=\"_blank\">(.*)<\/a>/isxU",$contents,$name);
		$name_arr = $name[1];
		foreach($name_arr as $key=>$v){
			
			$name_arr[$key] = html_entity_decode($v,ENT_QUOTES,'UTF-8');
			//$name_arr[$key] = iconv("UTF-8","GB2312//IGNORE",$name_arr[$key]);
		}
		
		//采集URL
		$preg = "/<a href=\"http:\/\/www\.renren\.com\/profile\.do\?ref=searchresult_(.*)&q=/isxU";
		preg_match_all($preg,$contents,$tmp_url_arr);
		$url_arr = $tmp_url_arr[1];
		//print_r($url_arr);
		foreach($url_arr as $key => $value){
			$name = $name_arr[$key];
			$s_id = explode('&id=',$value);
			$home_url = "http://www.renren.com/".$s_id[1]."/profile";
			
			//判断数据库是否已存在
			$query_check = mysql_query("SELECT `home_url` FROM `renren_hunauhomeurl` WHERE `home_url` = '$home_url'") or die(mysql_error());
			$check_num = mysql_num_rows($query_check);
			if($check_num>0){continue;}
			
			$detail_content = doget($home_url,"http://browse.renren.com/search.do?ref_search=searchResult_ReSearch",$cookies);
			$detail_content = tansHTML($detail_content);
			//echo $detail_content;
			/****** 获得用户性别 ******/
			//$preg = "/href=\"http:\/\/gift\.renren\.com\/send\.do?fid=277974863\">送(.*)礼物/isU";
			//if(preg_match($preg,$detail_content,$sex_arr)){
			//	if($sex_arr[1]=="她"){
			//		$sex = '女';
			//	}else{
			//		$sex = '男';
			//	}
			//}else{
			//	$preg = "/class=\"blog\">(.*)的日志/isU";
			//	preg_match($preg,$detail_content,$sex_arr);
			//	if($sex_arr[1]=="她"){
			//		$sex = '女';
			//	}else{
			//		$sex = '男';
			//	}
			//}
			//if($sex == '男'){continue;}
			$sex = '女';
			
			/****** 获得用户头像 ******/
			$avatar_pre = "/<imgstats=\"pri2_photo\"id=\"userpic\"onload=\"photoLoaded=true;\"src=\"(.*)\"/isxU";
			if(preg_match($avatar_pre,$detail_content,$avatar_arr)){
				$avatar_url = $avatar_arr[1];
			}else{
				$avatar_pre = "/<imgstats=\"pf_headalbum\"src='(.*)'/isxU";
				preg_match($avatar_pre,$detail_content,$avatar_arr);
				$avatar_url = $avatar_arr[1];
			}
			//echo $home_url."<br>".$name.$sex."<br>";
			
			//foreach ($img_arr as $key => $value) {
			if(file_get_contents($avatar_url)) $get_file = file_get_contents($avatar_url);
			else dir("出错");
			$filetime = time();
			$filepath = "D:\\wamp\\www\\caiji-hy\\renren\\pic\\";
			!is_dir($filepath) ? mkdir($filepath) : null;
			$filename = date("YmdHis",$filetime).rand(100,999).'_'.$_GET['num'].'.'.substr($avatar_url,-3,3);
			$fp = fopen($filepath.$filename,"w");
			fwrite($fp,$get_file);
			fclose($fp);
			//}
			
			$userid = 346293997+ $_GET['num'];
			/****** 信息入库  ******/
			$sql = "INSERT INTO `renren_hunauhomeurl` (`home_url`,`username`,`sex`,`avatar`,`avatar_url`,`status`,`userid`) VALUES ('$home_url','$name','$sex','$filename','$avatar_url',1,$userid)";
			mysql_query($sql)or die(mysql_error()) or die (mysql_error());
			$_GET['num']++;//照片计数器
		}
		
		echo "不要急~~~已经采了".($_GET['id']/10+1)."页啦...";
		$_GET['id'] += 10;
		echo "<script type=\"text/javascript\">location.href='http://facemash.com/collection/getHunauHomeUrl.php?id=".$_GET['id']."&num=".$_GET['num']."';</script>";
	}else{
		echo "采集完成！！";
	}
	function tansHTML($str){
		$str = trim($str); 
		//$str = strip_tags($str,""); 
		$str = str_replace("\t","",$str); 
		$str = str_replace("\r\n","",$str); 
		$str = str_replace("\r","",$str); 
		$str = str_replace("\n","",$str); 
		$str = str_replace(" ","",$str); 
		return trim($str); 
	}
?>